<?php

namespace app\common;

use Yii;
use yii\helpers\ArrayHelper;

class Util{
     
      //模拟登录
    public static function LoginSchool($number,$password){
         $url_login = Yii::$app->params['URL']['LOGIN'];//登录url
         $post = array(
              'userAccount'=>$number,
              'userPassword'=>$password,
              'encoded'=>base64_encode($number).'%%%'.base64_encode($password),
         );   //提交的数据
         
         $curl = curl_init();
         curl_setopt($curl,CURLOPT_URL,$url_login);
         curl_setopt($curl,CURLOPT_HEADER,1);//是否显示头信息
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 设定返回的数据是否自动显示
    	 curl_setopt($curl, CURLOPT_POST, 1); // post方式提交 
    	 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post)); // 要提交的信息 
    	 $result = curl_exec($curl); 
    	 curl_close($curl);
    	
		$result = mb_convert_encoding($result, 'utf-8', 'gbk'); // 转换字符集
		$array_result = explode("\r\n\r\n", $result);  // 解析HTTP数据流 
		preg_match("/set\-cookie:([^\r\n]*)/i", $array_result[0], $matches);  // 解析COOKIE  
		$cookie = $matches[1];  // 获取cookie

		// 判断密码是否有误
		if($result){
			$partten = '/<font.*?color="red">用户名或密码错误<\/font>/';
			preg_match($partten, $result, $data);
			if($data){
				echo json_encode(array('error'=>2,'data'=>'学号和密码有误'));
				Yii::$app->end();
			}
		}
		return $cookie;
	}

	// post请求，发送数据，需要cookie
	 public static function postDataByCookie($url, $cookie, $postData){
		$curl = curl_init(); 
		curl_setopt($curl, CURLOPT_URL, $url); 
		curl_setopt($curl, CURLOPT_HEADER, 0); // 是否显示头信息
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 设定返回的数据是否自动显示
		curl_setopt($curl, CURLOPT_POST, 1); // post提交
		curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postData)); //要提交的信息
		curl_setopt($curl, CURLOPT_COOKIE, $cookie); // 调取cookie信息
		$rs = curl_exec($curl);
		curl_close($curl); 
		return $rs; // 返回相应数据 
	}
	
	// get请求，需要cookie
	public static function getByCookie($url,$cookie){
		$curl = curl_init(); 
		curl_setopt($curl, CURLOPT_URL, $url); 
		curl_setopt($curl, CURLOPT_HEADER, 0); // 是否显示头信息
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 设定返回的数据是否自动显示
		curl_setopt($curl, CURLOPT_COOKIE, $cookie); // 调取cookie信息
		$rs = curl_exec($curl);
		curl_close($curl); 
		return $rs; // 返回相应数据
	}

	// get请求，SSL
        public static function getSSL($url){
		$curl = curl_init(); 
		curl_setopt($curl, CURLOPT_URL, $url); 
		curl_setopt($curl, CURLOPT_HEADER, 0); 
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
		$rs = curl_exec($curl);
		curl_close($curl); 
		return $rs; 
	}


	/**
	 * [转换字符集，去除空白标签]
	 * @param  [type] $html_str [处理前数据]
	 * @return [type]           [处理后数据]
	 */
	 public static function  deal_str($html_str){
		// 处理字符集
		$coding = mb_detect_encoding($html_str);
		if ($coding != "UTF-8" || !mb_check_encoding($html_str, "UTF-8"))
			$html_str = mb_convert_encoding($html_str, 'utf-8', 'GBK,UTF-8,ASCII');

		// 去除空标签
		$html_str = preg_replace("/[\t\n\r]+/", "", $html_str);

		return $html_str; // 返回数据
	}

	/**
	 * [去除指定标签]
	 * @param  [type] $tags [指定标签]
	 * @param  [type] $str  [处理数据]
	 * @return [type]       [description]
	 */
       public static function strip_html_tags($tags,$str){
		$html=array();
		foreach ($tags as $tag) {
			$html[]="/(<(?:\/".$tag."|".$tag.")[^>]*>)/i";
		}
		$data=preg_replace($html, '', $str); 
		return $data;
	}

    /**
     * [判断周末]
     * @return int
     */

	static function isWeekend(){
	    $data = date('w');
	    if(($data == 6) || ($data == 0)){
	        return 1;

        }else{}
        return 0;
    }

    static function  countTime($nowtime,$starttime){
	    $now = strtotime($nowtime);
	    $start = strtotime($starttime);
	    $res = $start - $now;
	    $res = floor($res/60);
	    return $res;
    }

    public static function deal_association_info($dataInfos){
        $associationSInfo = array();
        foreach($dataInfos as $value){
            $data = array(
                'associationDName' => $value->associationDName,
                'associationDImg'  => 'http://image.myzhbit.cn/associationImg/'.$value->associationDImg,
                'associationDUrl'  => $value->associationDUrl,
            );
            array_push($associationSInfo, $data);
        }
        return array(count($associationSInfo), $associationSInfo);
    }

    public static function deal_campustel($dataInfos){
	    $campusInfo = array();
	    foreach($dataInfos as $value){
            $data = ArrayHelper::toArray($value, [
                'app\models\CampusTel' => [
                    'telephoneName',
                    'telephoneNumber' ,
                ],
            ]);
            array_push($campusInfo, $data);
        }
        return array(count($campusInfo), $campusInfo);
    }


}


